package com.abel.bigwater.dataserver.model

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.dataserver.model.hgd.HgdUdpData
import com.abel.bigwater.dataserver.model.hgd.HgdaoBase
import com.abel.bigwater.dataserver.model.kming.KmingDataType
import com.abel.bigwater.dataserver.model.kming.TermSendData
import com.alibaba.fastjson.JSON
import io.netty.buffer.Unpooled
import org.junit.Assert.*
import org.junit.Test
import org.slf4j.LoggerFactory

class PojoHelperTest {
    @Test
    fun testIwg2Pojo2Kming() {
        val hgd = HgdUdpData(HgdaoBase.TYPE_TCP)
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(DEMO_HEX_STRING))

        assertTrue(hgd.parseBuffer(buf, true))

        val pojo = PojoHelper.toPojo(hgd, null)
        assertEquals(3, pojo.count.toInt())
        assertEquals(2, pojo.pulseList.size)
        assertEquals(1, pojo.analogList.size)

        val kpList = PojoHelper.toKmingPacket(pojo, null)

        val kp = kpList[1]
        assertEquals(KmingDataType.TermSendData, kp.data?.type)
        assertEquals(3, (kp.data as TermSendData).dataCount?.toInt())
        assertEquals(3, (kp.data as TermSendData).channelCount?.toInt())
        lgr.info("hgd->kming: ${JSON.toJSONString(kpList, true)}")

        val pojo2 = PojoHelper.toPojo(kpList[1])

        assertEquals(3, pojo2?.count?.toInt())
        assertEquals(3, pojo2?.pulseList?.firstOrNull()?.values?.size)
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(PojoHelperTest::class.java)

        const val DEMO_HEX_STRING = """AB CD 00 7D 00 66 11 05 24 16 15 03 0F 02 CE 00 05
        1E 44 31 31 35 3B 31 2E 34 3B 31 2E 30 2E 31 31 20 20 20 20 20 00 00 00 00 00 00 00 00
        01 01 0B 01 00 00 37 07 00 00 38 07 00 00 48 07 00 00
        01 02 0B 00 00 00 01 02 00 00 03 02 00 00 05 02 00 00
        02 03 01 01 00
        03 04 01 01 00
        04 05 3E 4C CC CD 3E 80 00 00 3E 8F 5C 29
        05 0D 00 12 00 13 40 11
        06 0E 00 12 67 89 00 12 64 89 00 32 67 80
        6A 0D 0A AB CD"""
    }
}